package main

import (
	_ "Monitor/app/routers"
	job "Monitor/job"
	"flag"
	"log"

	"github.com/astaxie/beego"
	"github.com/gogf/gf/frame/g"
	"github.com/kardianos/service"
)

var winlogger = service.ConsoleLogger

type program struct {
}

func (p *program) Start(s service.Service) error {
	if service.Interactive() {
		winlogger.Info("Running in terminal")
	} else {
		winlogger.Info("Running under service manager.")
	}
	//sync
	go p.run()
	return nil
}

func (p *program) Stop(s service.Service) error {
	winlogger.Info("I'm Stopping!")
	return nil
}

func (p *program) run() {
	winlogger.Info("I'm running %v.", service.Platform())
	serverRun()
}

func init() {
	g.Cfg().SetFileName("config.json")
}

//定义运行的任务的集合
func TaskRun() {
	job.PingRun()
	job.HttpRun()
	job.SendClientAddrRun()
}

//运行的任务
func serverRun() {
	TaskRun()
	beego.Run()
}

func main() {
	serviceAction := flag.String("service", "", "Control the system server")
	flag.Parse()
	svcConfig := &service.Config{
		Name:        "MonitorClient",
		DisplayName: "MonitorClient",
		Description: "Monitor client network",
	}

	prg := &program{}

	s, err := service.New(prg, svcConfig)
	if err != nil {
		log.Println(err)
	}
	if len(*serviceAction) != 0 {
		err := service.Control(s, *serviceAction)
		if err != nil {
			log.Printf("Valid actions: %q\n", service.ControlAction)
			log.Fatal(err)
		} else {
			log.Printf("actions: %q Success \n", service.ControlAction)
		}
		return
	}
	s.Run()
}
